package com.wandian.texas;

import java.util.List;
import com.wandian.texas.entity.CardVo;
import com.wandian.texas.entity.GameRoomEntity;
import com.wandian.texas.entity.UserGameEntity;
import com.wandian.texas.entity.WinCardEntity;

/**
 * 牌型计算公式
 * 
 * @author long
 * @date 2013-1-7 下午4:02:06
 * @qq 108232706
 * @description CalculateCard
 */
public final class CalculateCard {

	public final static void calculateWinMap(GameRoomEntity game, int bonusIndex) {
		if (bonusIndex > game.curBonusIndex)
			return;
		UserGameEntity[] winners = game.winnerMap[bonusIndex];
		int winIndex = 0;
		UserGameEntity winner;
		UserGameEntity temp;
		winner = winners[0];
		int total = game.winnerMapLen[bonusIndex];
		int result = 0;
//		boolean[] equals = new boolean[total];
//		equals[0] = true;
		for (int i = 1; i < total; i++) {
			temp = winners[i];
			//如果winner小于temp的牌
			result = temp.compareTo(winner);

			if (result > 0) {
				//clear equals and set true to null
				for (int j = 0; j < i; j++) {
					if (winners[j] != null) {
						winners[j] = null;
						game.winnerMapLen[bonusIndex]--;
					}

				}
//				winners[winIndex] = null;
				winIndex = i;
//				equals[i] = true;
				winner = winners[winIndex];
			} else if (result < 0) {
//				for (int j = 0; j < i; j++) {
//					if (equals[j]) {
//						winners[j] = null;
//						equals[j] = false;
//					}
//				}
				winners[i] = null;
//				equals[i] = true;
				game.winnerMapLen[bonusIndex]--;
			} else {
//				equals[i] = true;
			}
		}
		int behind = total - 1;
		//将非null的置前
		for (int front = 0; front < behind; front++) {
			//如果为null,从尾部获取
			if (winners[front] == null) {
				/*while (true) {
					if (winners[behind] != null)
						break;
					behind--;

				}*/
				for (; behind > front; behind--) {
					if (winners[behind] != null)
						break;
				}
				winners[front] = winners[behind];
				winners[behind] = null;
				behind--;
			}
		}
	}

	/**
	 * 随机洗牌
	 * @param arrHuase
	 */
	public final static void shuffle(String[] arrHuase) {

	}

	public final static boolean add(int[] listObj, int len, int value) {
		if (listObj.length <= len)
			return false;
		if (len == 0) {
			listObj[0] = value;
			return true;
		}
		int index = -1;
		for (int i = 0; i < len; i++) {
			if (listObj[i] >= value) {
				index = i;
				break;
			}
		}
		if (index == -1) {
			listObj[len] = value;
		} else {
			for (int i = len; i > index; i--) {
				listObj[i] = listObj[i - 1];
			}
			listObj[index] = value;
		}
		return true;
	}

	public final static boolean contains(int[] listObj, int len, int value) {
		return indexOf(listObj, len, value) != -1;
	}

	public final static boolean contains(int[] arr, int value) {
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == value)
				return true;
		}
		return false;
	}

	public final static boolean contains(int[] arr, int value, int begin, int end) {
		for (int i = begin; i < end; i++) {
			if (arr[i] == value)
				return true;
		}
		return false;
	}

	/**
	 * 对一组牌按大到小排序
	 * 
	 * @param arrHuase
	 */
	public final static void sortArrCard(CardVo[] arrHuase) {
		CardVo card;
		for (int i = 0; i < arrHuase.length - 1; i++) {
			for (int j = 0; j < arrHuase.length - i - 1; j++) {
				if (arrHuase[j].num < arrHuase[j + 1].num) {
					card = arrHuase[j];
					arrHuase[j] = arrHuase[j + 1];
					arrHuase[j + 1] = card;
				}
			}
		}
	}

	public final static int indexOf(int[] listObj, int len, int value) {
		for (int i = 0; i < len; i++) {
			if (listObj[i] == value)
				return i;
		}
		return -1;
	}

	public final static boolean remove(int[] listObj, int len, int value) {
		int index = indexOf(listObj, len, value);
		if (index != -1) {
			for (int i = index; i < len - 1; i++) {
				listObj[i] = listObj[i + 1];
			}
			listObj[len - 1] = 0;
			return true;
		}
		return false;
	}

	/**
	 * 桌面筹码大到小排序
	 * @param game
	 */
	public final static void sortSeatChip(GameRoomEntity game) {
		UserGameEntity user;
		UserGameEntity[] listObj = game.seatChipList;
		int len = game.SeatChipLen;
		for (int i = 0; i < len - 1; i++) {
			for (int j = 0; j < len - i - 1; j++) {
				if (listObj[j].totalBet < listObj[j + 1].totalBet) {
					user = listObj[j];
					listObj[j] = listObj[j + 1];
					listObj[j + 1] = user;
				}
			}
		}
	}

	public final static void sortAsc(int[] listObj, int len) {
		int temp;
		for (int i = 0; i < len - 1; i++) {
			for (int j = 0; j < len - i - 1; j++) {
				if (listObj[j] > listObj[j + 1]) {
					temp = listObj[j];
					listObj[j] = listObj[j + 1];
					listObj[j + 1] = temp;
				}
			}
		}
	}

	/**
	 * 排列一组,升序
	 * @param listObj
	 */
	public final static void sortAsc(List<Integer> listObj) {
		Integer temp;
		for (int i = 0; i < listObj.size() - 1; i++) {
			for (int j = i; j < listObj.size() - i - 1; j++) {
				if (listObj.get(j) > listObj.get(j + 1)) {
					temp = listObj.get(j);
					listObj.set(j, listObj.get(j + 1));
					listObj.set(j + 1, temp);
				}
			}
		}
	}

	/**
	 * @param cardTypeData2
	 *            坐位的牌型结果
	 * @param arrHuase
	 *            0表示全部的牌，1-4对应4种花色
	 * @param arrHuaseLen
	 *            对应arrHuase有效长度
	 * @param arrCards
	 *            游戏的公共牌
	 * @param handCards
	 *            手牌
	 * @return
	 */
	public final static void countCardType(GameRoomEntity game, WinCardEntity cardTypeData2, String[] arrCards, String[] handCards) {
		game.clearAllArrHuaseInfo();
		cardTypeData2.clear();
		int total = arrCards.length;
		int index = 0;
		CardVo cardVo = null;
		int huase;
		int num;
		CardVo[][] arrHuase = game.arrHuase;
		int[] arrHuaseLen = game.arrHuaseLen;
		// 将全部牌组织，不同的花色各自归队 -
		while (index < total) {
			if (arrCards[index] == "-") {
				continue;
			} else {
				if (arrHuaseLen[0] == 7) {
					System.err.println("1");
					System.err.println("1");
					System.err.println("1");
				}
				cardVo = arrHuase[0][arrHuaseLen[0]++];
				huase = countCardHuaSe(arrCards[index].substring(1, 2));
				num = countCardNum(arrCards[index].substring(0, 1));
				cardVo.cardStr = arrCards[index];
				cardVo.num = num;
				cardVo.huase = huase;
				cardVo = arrHuase[huase][arrHuaseLen[huase]++];
				cardVo.cardStr = arrCards[index];
				cardVo.num = num;
				cardVo.huase = huase;
			}
			index = index + 1;
		}
		for (int i = 0; i < handCards.length; i++) {
			if (handCards[i] == null)
				continue;
			cardVo = arrHuase[0][arrHuaseLen[0]++];
			huase = countCardHuaSe(handCards[i].substring(1, 2));
			num = countCardNum(handCards[i].substring(0, 1));
			cardVo.cardStr = handCards[i];
			cardVo.num = num;
			cardVo.huase = huase;
			cardVo = arrHuase[huase][arrHuaseLen[huase]++];
			cardVo.cardStr = handCards[i];
			cardVo.num = num;
			cardVo.huase = huase;
		}
		for (int i = 0; i < arrHuase.length; i++) {
			sortArrCard(arrHuase[i]);
		}
		// ---------------同花5
		boolean result = getFlush(cardTypeData2, arrHuaseLen, arrHuase);
		// ---------顺子4,同花顺8|皇家同花顺9
		if (result) {
			result = getStraight(cardTypeData2, false, game);
			if (result) {
				return;
			}
		}
		// --------------四条7
		result = getFourAkind(cardTypeData2, arrHuaseLen, arrHuase, game);
		if (result) {
			return;
		}

		// 葫芦6,三条3,两对2,一对1,高牌0
		result = getFullHouse(cardTypeData2, game);
		if (cardTypeData2.getType() == 6 || cardTypeData2.getType() == 5) {
			return;
		}
		// 顺子
		result = getStraight(cardTypeData2, true, game);

	}

	private final static void copyCard(CardVo from, CardVo to) {
		to.cardStr = from.cardStr;
		to.num = from.num;
		to.huase = from.huase;
	}

	/**
	 * 葫芦6,三条3,两对2,一对1,高牌0
	 * 
	 * @param arrCardVo
	 *            已排序的数组
	 * @return
	 */
	private final static boolean getFullHouse(WinCardEntity winCard, GameRoomEntity game) {
		CardVo tempCard = null;
		CardVo preCard = null;
		int[] arrHuaseLen = game.arrHuaseLen;
		int[] boolFullHouse = game.boolFullHouse;
		int[] AIndex = game.AIndex;
		CardVo[][] arrHuase = game.arrHuase;
		int arrLength = arrHuaseLen[0];
		game.clearHuaseLen();
		int index = 0;
		game.clearFullHouse();
		while (index < arrLength) {
			// [3,3,3,1,2]
			tempCard = arrHuase[0][index];
			if (index == 0) {
				copyCard(tempCard, arrHuase[1][arrHuaseLen[1]++]);
				boolFullHouse[arrHuaseLen[1] - 1]++;
				// [[3,3,3],[1],[2][5][7]]
			} else {
				preCard = arrHuase[0][index - 1];
				if (tempCard.num == preCard.num) {
					//					arrSame.get(arrSame.size() - 1).add(tempCard);
					//如。拷贝到2列的第 1列当前size-1索引，
					boolFullHouse[arrHuaseLen[1] - 1]++;//1变成2，相同数+1
					copyCard(tempCard, arrHuase[boolFullHouse[arrHuaseLen[1] - 1]][arrHuaseLen[1] - 1]);
				} else {
					//1列继续增加，同时该处相同牌数为1
					copyCard(tempCard, arrHuase[1][arrHuaseLen[1]++]);
					boolFullHouse[arrHuaseLen[1] - 1]++;
				}
			}
			index = index + 1;
		}
		int[] sameNum = game.sameNum;
		//统计每种牌的总个数，分别是1,2,3,4张相同的组合数
		for (int i = 0; i < sameNum.length; i++) {
			sameNum[i] = 0;
		}
		//将当前牌个数加进去,因为只会出0~4个
		for (int i = 0; i < arrHuaseLen[1]; i++) {
			if (boolFullHouse[i] == 1) {
				sameNum[0]++;
				if (arrHuase[1][i].num == 1)
					AIndex[0] = i;
			} else if (boolFullHouse[i] == 2) {
				sameNum[1]++;
				//1对A
				if (arrHuase[1][i].num == 1)
					AIndex[1] = i;
			} else if (boolFullHouse[i] == 3) {
				sameNum[2]++;
				//3个A
				if (arrHuase[1][i].num == 1)
					AIndex[2] = i;
			} else if (boolFullHouse[i] == 4) {
				sameNum[3]++;
				//4个A,情况不可能出现，在求4带1已经先确定
				if (arrHuase[1][i].num == 1)
					AIndex[4] = i;
			}
		}
		index = 0;
		//如果三张牌组合数与俩张牌组合数都大于1
		if (sameNum[2] >= 1 && sameNum[1] >= 1) {
			//如果存在3个A
			if (AIndex[2] != -1) {
				copyCard(arrHuase[1][AIndex[2]], winCard.arrMaxHandCards[0]);
				copyCard(arrHuase[2][AIndex[2]], winCard.arrMaxHandCards[1]);
				copyCard(arrHuase[3][AIndex[2]], winCard.arrMaxHandCards[2]);
				//				winCard.MaxHandCards(0, arrHuase[1][AIndex[2]]);
				//				winCard.MaxHandCards(1, arrHuase[2][AIndex[2]]);
				//				winCard.MaxHandCards(2, arrHuase[3][AIndex[2]]);
				winCard.CardLen += 3;
			}
			//如果存在1对A
			if (AIndex[1] != -1) {
				copyCard(arrHuase[1][AIndex[1]], winCard.arrMaxHandCards[3]);
				copyCard(arrHuase[2][AIndex[1]], winCard.arrMaxHandCards[4]);
				//				winCard.MaxHandCards(3, arrHuase[1][AIndex[1]]);
				//				winCard.MaxHandCards(4, arrHuase[2][AIndex[1]]);
				winCard.CardLen += 2;
			}
			//如果已经找到5张牌，通常是不会出现6个A，这里逻辑还是判断
			if (winCard.CardLen >= 5) {
				winCard.setType(6);
				return true;
			}
			if (sameNum[2] == 2) {
				//遍历不同牌个数，挑出3个的，加入进去
				for (int i = 0; i < arrHuaseLen[1]; i++) {
					if (boolFullHouse[i] == 3) {
						copyCard(arrHuase[1][i], winCard.arrMaxHandCards[winCard.CardLen++]);
						copyCard(arrHuase[2][i], winCard.arrMaxHandCards[winCard.CardLen++]);
						//						winCard.MaxHandCards(winCard.CardLen++, arrHuase[1][i]);
						//						winCard.MaxHandCards(winCard.CardLen++, arrHuase[2][i]);
						if (winCard.CardLen == 5)
							break;
						//两组牌都是3个，最后一张扔弃
						copyCard(arrHuase[3][i], winCard.arrMaxHandCards[winCard.CardLen++]);
						//						winCard.MaxHandCards(winCard.CardLen++, arrHuase[3][i]);
					}
				}
				winCard.setType(6);
				return true;
			}
			//遍历不同牌个数，挑出3个或2个的，加入进去
			for (int i = 0; i < arrHuaseLen[1]; i++) {
				//如果不存在3个A
				if (AIndex[2] == -1 && boolFullHouse[i] == 3) {
					copyCard(arrHuase[1][i], winCard.arrMaxHandCards[0]);
					copyCard(arrHuase[2][i], winCard.arrMaxHandCards[1]);
					copyCard(arrHuase[3][i], winCard.arrMaxHandCards[2]);
					//					winCard.MaxHandCards(0, arrHuase[1][i]);
					//					winCard.MaxHandCards(1, arrHuase[2][i]);
					//					winCard.MaxHandCards(2, arrHuase[3][i]);
					winCard.CardLen += 3;
				} else if (AIndex[1] == -1 && boolFullHouse[i] == 2) {//如果不存1对A
					copyCard(arrHuase[1][i], winCard.arrMaxHandCards[3]);
					copyCard(arrHuase[2][i], winCard.arrMaxHandCards[4]);
					//					winCard.MaxHandCards(3, arrHuase[1][i]);
					//					winCard.MaxHandCards(4, arrHuase[2][i]);
					winCard.CardLen += 2;
				}
				if (winCard.CardLen >= 5)
					break;
			}
			// 葫芦
			winCard.setType(6);
			return true;

		} else {
			if (winCard.getType() == 5 && winCard.CardLen >= 5)
				return false;
			if (sameNum[2] == 1) // 三条
			{
				//如果存在1张A
				if (AIndex[0] != -1) {
					copyCard(arrHuase[1][AIndex[0]], winCard.arrMaxHandCards[3]);
					//					winCard.MaxHandCards(4, arrHuase[1][AIndex[0]]);
					winCard.CardLen++;
				}
				//遍历不同牌个数，挑出3个的，加入进去
				for (int i = 0; i < arrHuaseLen[1]; i++) {
					if (boolFullHouse[i] == 3) {
						copyCard(arrHuase[1][i], winCard.arrMaxHandCards[0]);
						copyCard(arrHuase[2][i], winCard.arrMaxHandCards[1]);
						copyCard(arrHuase[3][i], winCard.arrMaxHandCards[2]);
						//						winCard.MaxHandCards(0, arrHuase[1][i]);
						//						winCard.MaxHandCards(1, arrHuase[2][i]);
						//						winCard.MaxHandCards(2, arrHuase[3][i]);
						winCard.CardLen += 3;
					} else if (boolFullHouse[i] == 1) {//如果是一只牌
						//未找到单牌.可之前找到或没有找到3张
						if (winCard.CardLen == 0 || winCard.CardLen == 3) {
							copyCard(arrHuase[1][i], winCard.arrMaxHandCards[3]);
							//							winCard.MaxHandCards(3, arrHuase[1][i]);
							winCard.CardLen++;
						} else if (winCard.CardLen == 1 || winCard.CardLen == 4) {
							copyCard(arrHuase[1][i], winCard.arrMaxHandCards[4]);
							//							winCard.MaxHandCards(4, arrHuase[1][i]);
							winCard.CardLen++;
						}
					}
					if (winCard.CardLen >= 5)
						break;
				}
				winCard.setType(3);
				return true;
			}
			winCard.clearHandCards();
			//如果存在1对A
			if (AIndex[1] != -1) {
				copyCard(arrHuase[1][AIndex[1]], winCard.arrMaxHandCards[0]);
				copyCard(arrHuase[2][AIndex[1]], winCard.arrMaxHandCards[1]);
				//				winCard.MaxHandCards(0, arrHuase[1][AIndex[1]]);
				//				winCard.MaxHandCards(1, arrHuase[2][AIndex[1]]);
				winCard.CardLen += 2;
			}
			if (sameNum[1] >= 2) // 两对
			{
				//如果存在1个A
				if (AIndex[0] != -1) {
					copyCard(arrHuase[1][AIndex[0]], winCard.arrMaxHandCards[4]);
					//					winCard.MaxHandCards(4, arrHuase[1][AIndex[0]]);
					winCard.CardLen += 1;
				}
				//遍历不同牌个数，挑出两对，加入进去，另外需要1张单牌
				for (int i = 0; i < arrHuaseLen[1]; i++) {
					if (boolFullHouse[i] == 2) {
						//如果没有加1对
						if (winCard.arrMaxHandCards[0].num == 0) {
							copyCard(arrHuase[1][i], winCard.arrMaxHandCards[0]);
							copyCard(arrHuase[2][i], winCard.arrMaxHandCards[1]);
							//							winCard.MaxHandCards(0, arrHuase[1][i]);
							//							winCard.MaxHandCards(1, arrHuase[2][i]);
							winCard.CardLen += 2;
						} else if (winCard.arrMaxHandCards[2].num == 0) {
							copyCard(arrHuase[1][i], winCard.arrMaxHandCards[2]);
							copyCard(arrHuase[2][i], winCard.arrMaxHandCards[3]);
							//							winCard.MaxHandCards(2, arrHuase[1][i]);
							//							winCard.MaxHandCards(3, arrHuase[2][i]);
							winCard.CardLen += 2;
						}
					} else if (winCard.arrMaxHandCards[4].num == 0 && boolFullHouse[i] == 1) {//如果是一只牌
						copyCard(arrHuase[1][i], winCard.arrMaxHandCards[4]);
						//						winCard.MaxHandCards(4, arrHuase[1][i]);
						winCard.CardLen++;

					}
					if (winCard.CardLen >= 5)
						break;
				}
				winCard.setType(2);
				return true;
			}
			if (sameNum[1] == 1) // 一对
			{
				//如果存在1个A
				if (AIndex[0] != -1) {
					copyCard(arrHuase[1][AIndex[0]], winCard.arrMaxHandCards[2]);
					//					winCard.MaxHandCards(2, arrHuase[1][AIndex[0]]);
					winCard.CardLen += 1;
				}
				//遍历不同牌个数，挑出1对，加入进去，另外需要3张单牌
				for (int i = 0; i < arrHuaseLen[1]; i++) {
					if (boolFullHouse[i] == 2) {
						//如果没有加1对
						if (winCard.arrMaxHandCards[0].num == 0) {
							copyCard(arrHuase[1][i], winCard.arrMaxHandCards[0]);
							copyCard(arrHuase[2][i], winCard.arrMaxHandCards[1]);
							//							winCard.MaxHandCards(0, arrHuase[1][i]);
							//							winCard.MaxHandCards(1, arrHuase[2][i]);
							winCard.CardLen += 2;
						}
					} else if (boolFullHouse[i] == 1) {//如果是一只牌
						if (winCard.arrMaxHandCards[2].num == 0) {
							copyCard(arrHuase[1][i], winCard.arrMaxHandCards[2]);
							//							winCard.MaxHandCards(2, arrHuase[1][i]);
							winCard.CardLen++;
						} else if (winCard.arrMaxHandCards[3].num == 0) {
							copyCard(arrHuase[1][i], winCard.arrMaxHandCards[3]);
							//							winCard.MaxHandCards(3, arrHuase[1][i]);
							winCard.CardLen++;
						} else if (winCard.arrMaxHandCards[4].num == 0) {
							copyCard(arrHuase[1][i], winCard.arrMaxHandCards[4]);
							//							winCard.MaxHandCards(4, arrHuase[1][i]);
							winCard.CardLen++;
						}

					}
					if (winCard.CardLen >= 5)
						break;
				}
				winCard.setType(1);
				return true;
			}
			// 高牌
			//如果存在1个A
			if (AIndex[0] != -1) {
				copyCard(arrHuase[1][AIndex[0]], winCard.arrMaxHandCards[0]);
				//winCard.MaxHandCards(0, arrHuase[1][AIndex[0]]);
				winCard.CardLen += 1;
			}
			//遍历不同牌个数，挑出5张单牌
			for (int i = 0; i < arrHuaseLen[1]; i++) {
				if (boolFullHouse[i] == 1) {//如果是一只牌
					copyCard(arrHuase[1][i], winCard.arrMaxHandCards[winCard.CardLen++]);
//					winCard.MaxHandCards(winCard.CardLen++, arrHuase[1][i]);
				}
				if (winCard.CardLen >= 5)
					break;
			}
			winCard.setType(0);
			return true;
		}
	}

	/**
	 * 四条7，计算全部牌里相同num的个数
	 * 
	 * @param arrCardVo
	 * @return
	 */
	private final static boolean getFourAkind(WinCardEntity winCard, int[] arrHuaseLen, CardVo[][] arrHuase, GameRoomEntity game) {
		CardVo tempCard = null;
		CardVo nextCard = null;
		int sameNumCount = 0;
		int arrCardlen = arrHuaseLen[0];
		int index = 0;
		// 4种花色对应arrHuase的下标，以及最大单牌的下标
		game.clearFourAKingIndex();
		int[] fourAKingIndex = game.fourAKingIndex;
		boolean hasA = arrHuase[0][arrHuaseLen[0] - 1].num == 1 ? true : false;
		while (index < arrCardlen) {

			tempCard = arrHuase[0][index];
			if ((index + 1) >= arrCardlen) {
				nextCard = arrHuase[0][index - 1];
				if (tempCard.num == nextCard.num) {
					// arrSame.add(tempCard);
					sameNumCount++;
					fourAKingIndex[tempCard.huase - 1] = index;
				} else {
					// fourAKingIndex[4] = index - 1;
				}
				break;
			}
			nextCard = arrHuase[0][index + 1];
			fourAKingIndex[tempCard.huase - 1] = index;
			// arrSame.add(tempCard);
			sameNumCount++;
			if (tempCard.num != nextCard.num) {
				if (sameNumCount < 4) {
					sameNumCount = 0;
					// arrSame.clear();
				} else {
					break;
				}
			}
			index = index + 1;
		}
		if (sameNumCount == 4) {
			winCard.CardLen = 0;
			// 4张同牌
			for (int i = 0; i < 4; i++) {
				winCard.arrMaxHandCards[winCard.CardLen++] = arrHuase[0][fourAKingIndex[i]];
			}
			// 取最大张牌
			if (hasA && winCard.arrMaxHandCards[0].num != 1) {
				//如果有A,并且不为四条，从最后一位拷贝
				copyCard(arrHuase[0][arrHuaseLen[0] - 1], winCard.arrMaxHandCards[winCard.CardLen++]);
			} else {
				for (int i = 0; i < arrHuaseLen[0]; i++) {
					if (arrHuase[0][i].num != winCard.arrMaxHandCards[0].num) {
//						winCard.arrMaxHandCards[winCard.CardLen++] = arrHuase[0][i];
						copyCard(arrHuase[0][i], winCard.arrMaxHandCards[winCard.CardLen++]);
						break;
					}
				}
//				copyCard(arrHuase[0][arrHuaseLen[0]-1], winCard.arrMaxHandCards[winCard.CardLen++]);
			}
			winCard.setType(7);
			return true;// new WinCardEntity(7, arrSame, null, arrCardVo,
						// singleCard);
		}
		return false;
	}

	/**
	 * //高牌0|一对1|两对2|三条3|顺子4|同花5|葫芦6|四条7|同花顺8|皇家同花顺9 <br/>
	 * 皇家同花顺9,同花顺8,顺子4
	 * 
	 * @param arrSomecolor
	 *            {"1h","2h","3h","4h","5h"}
	 * @param sameColorData
	 *            {type: 5, arr1: arrSomecolor, huase: huaseIndex:1-4};
	 * @return
	 */
	/**
	 * @param winCard
	 * @param arrHuaseLen
	 * @param arrHuase
	 * @param isShunzi
	 * @return
	 */
	private final static boolean getStraight(WinCardEntity winCard, boolean isShunzi, GameRoomEntity game) {
		CardVo[][] arrHuase = game.arrHuase;
		int[] arrHuaseLen = game.arrHuaseLen;
		CardVo cardVo = null;
		CardVo cardVo2 = null;
		CardVo tempCardVo = null;
		int huaseIndex = 0;
		if (!isShunzi) {
			huaseIndex = winCard.huase;
		}
		boolean hasA = false;
		if (arrHuase[huaseIndex][arrHuaseLen[huaseIndex] - 1].num == 1) {
			// 将0~n移动1格,少用了一次排序
			for (int i = arrHuaseLen[huaseIndex]; i >= 1; i--) {
				//				arrHuase[huaseIndex][i] = arrHuase[huaseIndex][i - 1];
				copyCard(arrHuase[huaseIndex][i - 1], arrHuase[huaseIndex][i]);
			}
			// cardVo = arrHuase[winCard.huase - 1][arrHuaseLen[winCard.huase -
			// 1]++];
			arrHuaseLen[huaseIndex]++;
			cardVo = arrHuase[huaseIndex][0];
			cardVo.cardStr = "A" + GamePool.arrHuase[huaseIndex];
			cardVo.num = 14;
			cardVo.huase = winCard.huase;
			hasA = true;

		}
		int index = 1;
		// 连续个数，防止破坏上次同花的结果
		int continuesCount = 1;
		// 为了标识哪些牌索引是连续的，
		boolean[] boolContinues = game.boolWinCards;
		boolContinues[0] = true;
		while (index < arrHuaseLen[huaseIndex]) {
			tempCardVo = arrHuase[huaseIndex][index];
			cardVo2 = arrHuase[huaseIndex][index - 1];
			// [5,4,4,3,2,1]
			// 后面的+1=前面的
			if ((cardVo2.num - 1) == tempCardVo.num) {
				continuesCount++;
//				boolContinues[index - 1] = true;
				boolContinues[index] = true;
				// winCard.arrMaxHandCards[winCard.CardLen++] = cardVo2;
			}
			// 如果值相等，继续
			else if (cardVo2.num == tempCardVo.num) {
				index++;
				continue;
			}
			// arrContinueCard[5,4,3,2,1]
			else if (continuesCount >= 5) {
				break;
			}
			// 前面的不符合，后面开始重新计算
			else {
				continuesCount = 1;
				for (int i = 0; i < boolContinues.length; i++) {
					boolContinues[i] = false;
				}
				boolContinues[index] = true;
//				boolContinues[index - 1] = true;
				// winCard.CardLen = 1;
				// winCard.arrMaxHandCards[winCard.CardLen] = cardVo2;
			}
			index++;
		}
		if (continuesCount >= 5) {
			winCard.CardLen = 0;
			for (int i = 0; i < boolContinues.length; i++) {
				if (boolContinues[i]) {
					//					winCard.arrMaxHandCards[winCard.CardLen++] = arrHuase[huaseIndex][i];
					//					winCard.MaxHandCards(winCard.CardLen++, arrHuase[huaseIndex][i]);
					copyCard(arrHuase[huaseIndex][i], winCard.arrMaxHandCards[winCard.CardLen++]);
					if (winCard.CardLen >= 5)
						break;
				}
			}
			if (!isShunzi) {
				// 14是临时增加的
				if (winCard.arrMaxHandCards[0].num == 14) {
					// 移除尾部的num=1,里面肯定不包括,最长是7
					// 皇家同花顺
					winCard.setType(9);
					winCard.huase = huaseIndex;
					// 移除最后一个A，总长度减1，把1变成14
					arrHuaseLen[huaseIndex]--;
					return true;
				}
				if (hasA) {
					// 移除第一个A，总长度减1，14不需要
					arrHuaseLen[huaseIndex]--;
					for (int i = 0; i < arrHuaseLen[huaseIndex]; i++) {
						//				arrHuase[huaseIndex][i] = arrHuase[huaseIndex][i + 1];
						copyCard(arrHuase[huaseIndex][i + 1], arrHuase[huaseIndex][i]);
					}
				}
				// 同花顺
				winCard.setType(8);
				winCard.huase = huaseIndex;
				return true;
			} else {
				// 顺子
				if (hasA) {
					//如果14用到，就移除1
					if (winCard.arrMaxHandCards[0].num == 14) {
						arrHuaseLen[huaseIndex]--;
					} else {
						// 移除第一个A，总长度减1，14不需要,从首位del
						arrHuaseLen[huaseIndex]--;
						for (int i = 0; i < arrHuaseLen[huaseIndex]; i++) {
							//				arrHuase[huaseIndex][i] = arrHuase[huaseIndex][i + 1];
							copyCard(arrHuase[huaseIndex][i + 1], arrHuase[huaseIndex][i]);
						}
					}
				}
				winCard.setType(4);
				winCard.huase = -1;
				return true;
			}
		}
		//求顺子的时候，如果不是，不影响上次FullHouse的结果，保持winCard不变
		return false;
	}

	/**
	 * 同花
	 * 
	 * @param arrHuase
	 *            [[{"1h","2h","3h","4h","5h"}],[],[],[]]
	 * @return
	 */
	private final static boolean getFlush(WinCardEntity winCard, int[] arrHuaseLen, CardVo[][] arrHuase) {
		int huaseLen = 0;
		// ArrayList<CardVo> copySameColor = new ArrayList<CardVo>();
		int huaseIndex = 0;
		int index = 1;// 0列是全部牌
		winCard.CardLen = 0;
		while (index < arrHuase.length) {
			huaseLen = arrHuaseLen[index];
			if (huaseLen >= 5) {
				huaseIndex = index;
				//如果同花里有A，是最大
				if (arrHuase[index][huaseLen - 1].num == 1) {
					copyCard(arrHuase[index][huaseLen - 1], winCard.arrMaxHandCards[0]);
//					winCard.MaxHandCards(0, arrHuase[index][huaseLen - 1]);
					winCard.CardLen++;
					for (int i = 0; i < 4; i++) {
						copyCard(arrHuase[index][i], winCard.arrMaxHandCards[i + 1]);
//						winCard.MaxHandCards(i + 1, arrHuase[index][i]);
						winCard.CardLen++;
					}
				} else {
					for (int i = 0; i < 5; i++) {
						copyCard(arrHuase[index][i], winCard.arrMaxHandCards[i]);
//						winCard.MaxHandCards(i, arrHuase[index][i]);
						winCard.CardLen++;
					}
				}
				break;
			}
			index++;
		}
		if (huaseIndex == 0) {
			return false;
		}
		winCard.setType(5);
		winCard.huase = huaseIndex;
		return true;
		// return new WinCardEntity(5, huaseIndex, copySameColor,
		// copySameColor);
	}

	private final static int countCardNum(String cardvalue) {
		int frame = 0;
		if (cardvalue.equals("A")) {
			frame = 1;
		} else if (cardvalue.equals("T")) {
			frame = 10;
		} else if (cardvalue.equals("J")) {
			frame = 11;
		} else if (cardvalue.equals("Q")) {
			frame = 12;
		} else if (cardvalue.equals("K")) {
			frame = 13;
		} else {
			frame = Integer.valueOf(cardvalue);
		}
		return frame;
	}

	private final static int countCardHuaSe(String huase) {
		int frame = 0;
		if ("d".equals(huase)) {// 方块
			frame = 1;
		} else if ("c".equals(huase)) {// 梅花
			frame = 2;
		} else if ("h".equals(huase)) {// 红桃
			frame = 3;
		} else if ("s".equals(huase)) {// 黑桃
			frame = 4;
		}
		return frame;
	}
}
